package com.hanson.basicknowledge.mybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hanson.basicknowledge.mybatisplus.domain.User;
import com.hanson.basicknowledge.mybatisplus.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Hanson
 * @date 2023/2/24 17:29
 */
@Controller
@RequestMapping(value = "/test")
public class TestController {

    @Resource
    private UserMapper userMapper;

    /**
     * 新增用户
     */
    @PostMapping("/add")
    public void insertUser(){
        User user = new User();
        user.setName("张三");
        user.setAge(18);
        user.setEmail("zhangsan@126.com");
        userMapper.insert(user);
    }

    /**
     *
     // 根据主键查询用户
     *
     * @return user
     */
    @GetMapping("/selectById")
    public User selectById(){
        return userMapper.selectById(1L);
    }

    /**
     * 根据条件查询用户
     *
     * @return
     */
    @GetMapping("/select")
    public List<User> selectByUser(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name","张三");
        List<User> userList = userMapper.selectList(wrapper);
        return userList;
    }

    /**
     * 更新
     */
    @PostMapping("/update")
    public void updateUser(){
        User user = new User();
        user.setId(1L);
        user.setAge(20);
        userMapper.updateById(user);
    }


    /**
     * 删除用户
     */
    @DeleteMapping("/delete")
    public void deleteUser(){
        userMapper.deleteById(1L);
    }


}
